﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Management;
using System.Text;
using WMI.NET.Abstracts;
using WMI.NET.CIM;

namespace WMI.NET.OperatingSystem.Users
{
    /// <summary>
    /// The Win32_Account abstract WMI class contains information about user accounts and group accounts known to the computer system running Windows. User or group names recognized by a Windows domain are descendants (or members) of this class.
    /// </summary>
    public class Win32_Account : CIM_LogicalElement
    {

        private string _Domain;
        private Nullable<Boolean> _LocalAccount;
        private string _SID;
        private Nullable<byte> _SIDType;

        /// <summary>
        /// Name of the Windows domain to which a group or user belongs.
        /// Example: "NA-SALES" 
        /// </summary>
        public string Domain { get { return _Domain; } }

        /// <summary>
        /// If TRUE, the account is defined on the local machine. To retrieve only accounts defined on the local machine, design a query that includes the condition "LocalAccount=TRUE". 
        /// </summary>
        public Nullable<Boolean> LocalAccount { get { return _LocalAccount; } }

        /// <summary>
        /// Security identifier (SID) for this account. A SID is a string value of variable length used to identify a trustee. Each account has a unique SID issued by an authority (such as a Windows domain), stored in a security database. When a user logs on, the system retrieves the user's SID from the database and places it in the user's access token. The system uses the SID in the user's access token to identify the user in all subsequent interactions with Windows security. When a SID has been used as the unique identifier for a user or group, it cannot be used again to identify another user or group.
        /// </summary>
        public string SID { get { return _SID; } }

        /// <summary>
        /// Enumerated values that specify the type of security identifier (SID).
        /// </summary>
        public Nullable<byte> SIDType { get { return _SIDType; } } //TODO enum

        /// <summary>
        /// 
        /// </summary>
        /// <param name="mbo"></param>
        public Win32_Account(ManagementBaseObject mbo)
            : base(mbo)
        {
            _Domain = mbo.GetStringValue("Domain");
            _LocalAccount = mbo.GetBoolValue("LocalAccount");
            _SID = mbo.GetStringValue("SID");
            _SIDType = mbo.GetByteValue("SIDType");
        }


    }

    /// <summary>
    /// 
    /// </summary>
    public class GetWin32_Accounts : Win32Collection<Win32_Account>
    {

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override ReadOnlyCollection<Win32_Account> GetCollection()
        {
            List<Win32_Account> tempCollection = new List<Win32_Account>();

            foreach (var s in searcher.Get())
            {
                Win32_Account tempListItem = new Win32_Account(s);
                tempCollection.Add(tempListItem);
            }

            return tempCollection.AsReadOnly();
        }

        /// <summary>
        /// 
        /// </summary>
        public GetWin32_Accounts() : base("SELECT * FROM Win32_Account") { }

    }

}
